package com.briup.day21;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

public class Test02_Sep {

    // ? super E
    // ? extends E
    public static void main(String[] args) {
        // ? extends Number 可以传递Number及其子类
        Collection<? extends Number> coll = null;
        coll = new ArrayList<Number>();
        coll = new ArrayList<Integer>();
        coll = new ArrayList<Double>();
        coll = new ArrayList<Float>();
        //coll = new ArrayList<String>(); //ERROR

        // ? super Number 可以传递Number及其父类|接口
        Collection<? super Number> coll2 = null;
        coll2 = new ArrayList<Number>();
        String className = coll2.getClass().getName();
        System.out.println("new ArrayList<Number>(): " + className);

        coll2 = new ArrayList<Object>();
        className = coll2.getClass().getName();
        System.out.println("new ArrayList<Object>(): " + className);

        coll2 = new ArrayList<Serializable>();
        className = coll2.getClass().getName();
        System.out.println("new ArrayList<Serializable>(): " + className);

        //coll2 = new ArrayList<Integer>(); ERROR
    }

    //? 特殊用法 面试题
    public static void main01(String[] args) {
        Collection<String> c1 = new ArrayList<>();
        c1.add("hello");

        Collection<?> c2 = new ArrayList<String>();
        //c2.add("world"); ERROR
        c2.add(null);      // OK
    }
}
